//
//  GHUtils.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLUtils.h"

@implementation KLUtils
+ (void)netWorkMonitorinCanUseWithHandle:(void (^)())handle{
    
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
            if (handle) {
                handle();
            }
        }
    }];
    [manager startMonitoring];
}
+ (void)sendQiNiuWithImage:(UIImage *)image handle:(void (^)(NSString *, NSString *))handle{
   // [SVProgressHUD showWithStatus:@"请稍后"];
    NSData * data = UIImageJPEGRepresentation(image, 0.7);
    UIImage *  dealImage = [UIImage imageWithData:data];
    if ([STUserManger defult].upload_tk.length) {
        
        NSString *token = [STUserManger defult].upload_tk;
        NSString * filePath = [KLUtils getImagePath:dealImage];
        QNUploadManager *upManager = [[QNUploadManager alloc] init];
        QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {
            NSLog(@"percent == %.2f", percent);
        }
                                                                     params:nil
                                                                   checkCrc:NO
                                                         cancellationSignal:nil];
        [upManager putFile:filePath key:nil token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
            NSLog(@"info ===== %@", info);
            NSLog(@"resp ===== %@", resp);
            if ([resp.allKeys containsObject:@"key"]) {
                NSString * imageUrl = [NSString stringWithFormat:@"%@%@",QNImageHeader,resp[@"key"]];
                if (handle) {
                    handle(imageUrl,nil);
                }
            }
        }
                    option:uploadOption];
        
    }else{
        if (handle) {
            handle(nil,@"七牛 token 为空");
        }
    }
}
//照片获取本地路径转换
+ (NSString *)getImagePath:(UIImage *)Image {
    NSString *filePath = nil;
    NSData *data = nil;
    if (UIImagePNGRepresentation(Image) == nil) {
        data = UIImageJPEGRepresentation(Image, 0.5);
    } else {
        data = UIImagePNGRepresentation(Image);
    }
    
    //图片保存的路径
    //这里将图片放在沙盒的documents文件夹中
    NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    
    //文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSInteger num = arc4random()%10000000;
    //把刚刚图片转换的data对象拷贝至沙盒中
    [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *ImagePath = [NSString stringWithFormat:@"/theFirstImage%ld.png",num];
    [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:ImagePath] contents:data attributes:nil];
    
    //得到选择后沙盒中图片的完整路径
    filePath = [[NSString alloc] initWithFormat:@"%@%@", DocumentsPath, ImagePath];
    return filePath;
}

+ (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    sectionHeader.backgroundColor = [UIColor whiteColor];
    STButton * line = [[STButton alloc] initWithFrame:CGRectMake(15, 0, 4, 22)
                                                title:nil
                                           titleColor:nil
                                            titleFont:14
                                         cornerRadius:0
                                      backgroundColor:KL_BlueBackGroundColor
                                      backgroundImage:nil
                                                image:nil];
    line.centerY = 22;
    [sectionHeader addSubview:line];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(line.right + 10, 0, 200, 44)
                                                     text:title
                                                textColor:KL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    
    [sectionHeader addSubview:titleLable];
    return sectionHeader;
}
+ (CGFloat)navgationBarBootom{
    if (KIsiPhoneX) {
        return 88;
    }
    return 64;
}
+ (CGFloat)tabBarTop{
    if (KIsiPhoneX) {
        return (UIScreenHeight - 83);
    }
    return (UIScreenHeight - 49);
}
+  (void)fetch_MineInfoWithHandle:(void (^)(KLUserModel *, STError *))handle{
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/user/userinfo",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"userinfo"]) {
                                                     KLUserModel * user = [KLUserModel mj_objectWithKeyValues:dataDic[@"userinfo"]];
                                                     if (handle) {
                                                         handle(user,nil);
                                                     }
                                                 }

                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 if (handle) {
                                                     handle(nil,error);
                                                 }
                                             }];

    
}
+ (void)fecthGoodDetailWithID:(NSString*)goodID hanlde:(void(^)(KLGoodDetailModel*model,STError * error))handle{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/goods/goodsinfo"];
    if (goodID.length) {
        [dic setObject:goodID forKey:@"goods_id"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 KLGoodDetailModel * goooDetailModel;
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"info"]) {
                                                     goooDetailModel = [KLGoodDetailModel mj_objectWithKeyValues:dataDic[@"info"]];
                                                     
                                                     goooDetailModel.shops = [KLShopModel mj_objectWithKeyValues:dataDic[@"info"][@"shops"]];
                                                 }
                                                 if (handle) {
                                                     handle(goooDetailModel,nil);
                                                 }

                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 if (handle) {
                                                     handle(nil,error);
                                                 }
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
